package client.gui.lobby;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.util.ArrayDeque;
import java.util.Deque;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import lib.Commands;
import client.Client;
import client.TCPClientListener;

public class Lobby extends JFrame implements Client {
	/**
	 * 
	 */
	private static final long serialVersionUID = -122242570361788884L;
	TCPClientListener listener = null; // @jve:decl-index=0:
	private JPanel contentPanel = null;
	private ChatPanel chatPanel = null;
	private JoinPanel joinPanel = null;
	private JSplitPane jSplitPane = null;
	private String host = "", login = "", password = ""; // @jve:decl-index=0:
	private int port = 11011; // set to default
	public Deque<String> Users=new ArrayDeque<String>();
	public Lobby() {
		initialize();
	}

	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(new Dimension(462, 414));
		this.setPreferredSize(new Dimension(400, 400));
		this.setContentPane(getContent());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.out.println("windowClosing()"); // TODO Auto-generated
				// Event stub
				// windowClosing()
				System.exit(0);
			}
		});
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getContent() {
		if (contentPanel == null) {
			contentPanel = new JPanel();
			contentPanel.setComponentOrientation(ComponentOrientation.UNKNOWN);
			contentPanel.setLayout(new BorderLayout());
			contentPanel.add(getJSplitPane(), BorderLayout.CENTER);
		}
		return contentPanel;
	}

	/**
	 * This method initializes chatPanel
	 * 
	 * @return client.gui.lobby.ChatPanel
	 */
	private ChatPanel getChatPanel() {
		if (chatPanel == null) {
			chatPanel = new ChatPanel(this);
		}
		return chatPanel;
	}

	/**
	 * This method initializes joinFrame
	 * 
	 * @return client.gui.lobby.JoinFrame
	 */
	private JoinPanel getJoinFrame() {
		if (joinPanel == null) {
			joinPanel = new JoinPanel(this);
			joinPanel.setVisible(true);
		}
		return joinPanel;
	}

	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			jSplitPane.setVisible(true);
			jSplitPane.setTopComponent(getJoinFrame());
			jSplitPane.setBottomComponent(getChatPanel());
			
		}
		return jSplitPane;
	}

	public void setConnectionDetails(String host, int port, String login, String password) {
		this.host = host;
		this.port = port;
		this.login = login;
		this.password = password;
	}

	public boolean connect() {
		try {
			listener = new TCPClientListener(this, host, port);
			listener.gui = this;
			listener.start();
			listener.send(Commands.LOGIN, login + " " + password);
			Thread.sleep(1000);
			listener.send(Commands.JOIN, " ");
			listener.send(Commands.JOIN, " ");
			
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	int UniversNR=0;
	public void displayInfo(Deque<String> msg) {
			if (msg.peekLast().equals(("STATUS"))) {
				msg.pollLast();
				joinPanel.UniversesInfo[UniversNR] += "\n" + listener.getMessage(msg);
				joinPanel.jTextArea.setText(joinPanel.UniversesInfo[joinPanel.jComboBox.getSelectedIndex()]);
				return;
			}
			UniversNR = Integer.parseInt(msg.pollLast()) - 1;
			if(UniversNR==0){
				joinPanel.jComboBox.removeActionListener(joinPanel.comboboxListerner);
				joinPanel.jComboBox.removeAllItems();
				joinPanel.jComboBox.addActionListener(joinPanel.comboboxListerner);
			
			}
			String s = listener.getMessage(msg);
			joinPanel.UniversesInfo[UniversNR] = s;
			joinPanel.jComboBox.addItem(s);		
	}

	
	public void displayMessage(String msg) {
		// TODO Auto-generated method stub
		chatPanel.displayMessage(msg);
	}

	
	public void exit() {
		// TODO Auto-generated method stub
		if (listener != null)
			listener.send(Commands.LEAVE, "");
	}

	
	public void send(Commands cmd, String msg) {
		listener.send(cmd, msg);
	}

	
	public void progress(String msg,int i) {
		joinPanel.jProgressBar.setValue(i);
		repaint();
		
	}

	public void list(Deque<String> msg) {
		if(msg.peekLast().equals("CLEAR")){
			Users.clear();
			return;
		}
		Users.push(msg.pollLast());		
		chatPanel.updateUserList(Users);
		
	}
} // @jve:decl-index=0:visual-constraint="10,10"
